package com.kurumi.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kurumi.mapper.MedicalRecordMapper;
import com.kurumi.query.MedicalRecordQuery;
import com.kurumi.service.MedicalRecordCheckService;
import com.kurumi.util.JsonUtil;
import com.kurumi.util.PageIndexCheckUtil;
import com.kurumi.util.PageIndexCheckUtils;
import com.kurumi.util.StringUtil;

@Service
public class MedicalRecordCheckServiceImpl implements MedicalRecordCheckService {

	@Autowired
	private MedicalRecordMapper medicalRecordMapper;


	@Override
	public List<Map<String, Object>> pageIndexCheck(MedicalRecordQuery medicalRecordQuery) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> errorMedicalRecords = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> medicalRecords = medicalRecordMapper.getMedicalRecordOfCheck(medicalRecordQuery);
		for (Map<String, Object> medicalRecord : medicalRecords) {
			String traceTypeCodes = (String)medicalRecord.get("trace_type_codes");
			List<String> errorDescs = new ArrayList<String>();
			StringBuilder errorDesc = new StringBuilder();
			if(traceTypeCodes == null){
				if(errorDesc.length()> 0){
					errorDesc.append(System.getProperty("line.separator"));
				}
				errorDesc.append("首页未完成编码");
				if(errorDesc.length()> 0){
					errorDesc.append(System.getProperty("line.separator"));
				}
				medicalRecord.put("error_desc", errorDesc.toString());
				errorDescs.add("首页未完成编码");
				medicalRecord.put("check_desc", errorDescs);
				errorMedicalRecords.add(medicalRecord);
				continue;
			}else if(!traceTypeCodes.contains("BABM")){
				if(errorDesc.length()> 0){
					errorDesc.append(System.getProperty("line.separator"));
				}
				errorDesc.append("首页未完成编码");
				if(errorDesc.length()> 0){
					errorDesc.append(System.getProperty("line.separator"));
				}
				medicalRecord.put("error_desc", errorDesc.toString());
				errorDescs.add("首页未完成编码");
				medicalRecord.put("check_desc",errorDescs);
				errorMedicalRecords.add(medicalRecord);
				continue;
			}
			String visitGuid = (String)medicalRecord.get("visit_guid");
			List<String> jsonDatas = medicalRecordMapper.getMedicalRecordJsonByVisitGuid(StringUtil.handleJsonParam(visitGuid));

			if(!jsonDatas.isEmpty()){
				Map<String, Object> jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				Map<String, Object> basicInfo = (Map<String, Object>)jsonMap.get("basicInfo");
				Map<String, Object> cureInfo = (Map<String, Object>)jsonMap.get("cureInfo");
				Map<String, Object> diseaseDiagInfo = (Map<String, Object>)jsonMap.get("hqmsDiseaseDiagInfo");
				Map<String, Object> operateInfo = (Map<String, Object>)jsonMap.get("hqmsOperateInfo");
				Map<String,String> check = PageIndexCheckUtils.checkHQMS1(basicInfo);
				Map<String, Object> intensiveCareInfo = (Map<String, Object>)jsonMap.get("intensiveCareInfo");
				Map<String, Object> costInfo = (Map<String, Object>)jsonMap.get("costInfo");
				if(check!= null){
					if(errorDesc.length()> 0){
						errorDesc.append(System.getProperty("line.separator"));
					}
					errorDesc.append(check.get("errorDesc"));
					errorDescs.add(check.get("errorDesc"));
				}
				check = PageIndexCheckUtils.checkHQMS2(basicInfo, operateInfo);
				if(check!= null){
					if(errorDesc.length()> 0){
						errorDesc.append(System.getProperty("line.separator"));
					}
					errorDesc.append(check.get("errorDesc"));
					errorDescs.add(check.get("errorDesc"));
				}
				check = PageIndexCheckUtils.checkHQMS3_1(basicInfo, cureInfo, diseaseDiagInfo);
				if(check!= null){
					if(errorDesc.length()> 0){
						errorDesc.append(System.getProperty("line.separator"));
					}
					errorDesc.append(check.get("errorDesc"));
					errorDescs.add(check.get("errorDesc"));
				}
				check = PageIndexCheckUtils.checkHQMS3(basicInfo, cureInfo, diseaseDiagInfo);
				if(check!= null){
					if(errorDesc.length()> 0){
						errorDesc.append(System.getProperty("line.separator"));
					}
					errorDesc.append(check.get("errorDesc"));
					errorDescs.add(check.get("errorDesc"));
				}
				check = PageIndexCheckUtils.checkHQMS4(basicInfo, cureInfo);
				if(check!= null){
					if(errorDesc.length()> 0){
						errorDesc.append(System.getProperty("line.separator"));
					}
					errorDesc.append(check.get("errorDesc"));
					errorDescs.add(check.get("errorDesc"));
				}
				check = PageIndexCheckUtils.checkHQMS5(basicInfo);
				if(check!= null){
					if(errorDesc.length()> 0){
						errorDesc.append(System.getProperty("line.separator"));
					}
					errorDesc.append(check.get("errorDesc"));
					errorDescs.add(check.get("errorDesc"));
				}
				check = PageIndexCheckUtils.checkHQMS6(basicInfo, intensiveCareInfo);
				if(check!= null){
					if(errorDesc.length()> 0){
						errorDesc.append(System.getProperty("line.separator"));
					}
					errorDesc.append(check.get("errorDesc"));
					errorDescs.add(check.get("errorDesc"));
				}
				List<Map<String,String>> errors = PageIndexCheckUtils.checkHQMS7(costInfo);
				if(errors!= null){
					for (Map<String, String> error : errors) {
						if(errorDesc.length()> 0){
							errorDesc.append(System.getProperty("line.separator"));
						}
						errorDesc.append(error.get("errorDesc"));
						errorDescs.add(error.get("errorDesc"));
					}

				}
				check = PageIndexCheckUtils.checkHQMS12(basicInfo, cureInfo);
				if(check!= null){
					if(errorDesc.length()> 0){
						errorDesc.append(System.getProperty("line.separator"));
					}
					errorDesc.append(check.get("errorDesc"));
					errorDescs.add(check.get("errorDesc"));
				}
				check = PageIndexCheckUtils.checkHQMS13(basicInfo, cureInfo, diseaseDiagInfo);
				if(check!= null){
					if(errorDesc.length()> 0){
						errorDesc.append(System.getProperty("line.separator"));
					}
					errorDesc.append(check.get("errorDesc"));
					errorDescs.add(check.get("errorDesc"));
				}
			}else{
				if(errorDesc.length()> 0){
					errorDesc.append(System.getProperty("line.separator"));
				}
				errorDesc.append("没有首页信息");
				errorDescs.add("没有首页信息");
				medicalRecord.put("check_desc",errorDescs);
				errorMedicalRecords.add(medicalRecord);
			}
			if(!errorDescs.isEmpty()){
				medicalRecord.put("error_desc", errorDesc.toString());
				medicalRecord.put("check_desc", errorDescs);
				errorMedicalRecords.add(medicalRecord);
			}
		}
		return errorMedicalRecords;

	}

}
